﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Wpf2.Models;
using Wpf2.ViewModels;

namespace Wpf2.Views
{
    /// <summary>
    /// Interaction logic for formProductHidden.xaml
    /// </summary>
    public partial class FormProductHidden 
    {
        readonly NotifiableCollection<Products> _products;
        readonly IList<Products> _productFirstLoad;
        public FormProductHidden()
        {
            try
            {
                InitializeComponent();
                _products = new NotifiableCollection<Products>();
                _productFirstLoad = new List<Products>();
                var listPro = WPFServices.Services.Products.GetListProductsHidden().Select(p => new Products
                {
                    ProductIdAuto = p.ID_PRO_AUTO,
                    ProductId = p.ID_PRO,
                    ProductName = p.NAME_PRO,
                    ProductDescription = p.DESCRIPTIONS,
                    CostPrice = p.DETAILS_PRODUCTS.COST_PRICES ?? 0,
                    SellPrice = p.DETAILS_PRODUCTS.SELL_PRICE ?? 0,
                    InventoryCount = p.DETAILS_PRODUCTS.INVENTORY_COUNT ?? 0,
                    CatalogId = p.ID_CATALOG,
                    UnitId = p.DETAILS_PRODUCTS.ID_UNITS,
                    CatalogName = p.CATALOG.NAME_CATALOG,
                    UnitName = p.DETAILS_PRODUCTS.UNIT.NAME_UNIT

                });
                foreach (var p in listPro)
                {
                    _products.Add(p);
                    _productFirstLoad.Add(p);
                }
                dtgProduct.DataContext = _products;
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        private void DtgProductLoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString(CultureInfo.InvariantCulture);
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            if (button == null) return;
            var p = button.DataContext as Products;
            if (p == null || p.ProductIdAuto == 0) return;
            try
            {
                WPFServices.Services.Products.ChangeStatus(p.ProductIdAuto, true);
                var listtem = _products.ToList();
                listtem.Remove(p);
                _products.Clear();
                foreach (var it in listtem)
                {
                    _products.Add(it);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void TxtIdProTextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                var searchString = txtIDPro.Text;
                cmbSearchByID.Items.Clear();
                foreach (var cbmItem in from pro in _productFirstLoad where pro.ProductId.ToUpper().IndexOf(searchString.ToUpper(), StringComparison.Ordinal) >= 0 && !string.IsNullOrEmpty(searchString) select new ComboBoxItem {Content = pro.ProductId, Tag = pro.ProductIdAuto})
                {
                    cmbSearchByID.Items.Add(cbmItem);
                }
                cmbSearchByID.IsDropDownOpen = cmbSearchByID.HasItems;
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        private void ComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var comboBox = sender as ComboBox;
                if (comboBox != null && comboBox.SelectedItem == null) return;
                var box = sender as ComboBox;
                if (box != null)
                {
                    var name = box.SelectedItem as ComboBoxItem;
                    if (name != null) txtIDPro.Text = name.Content.ToString();
                }
                btnSearchbyid.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
            }
            catch (Exception ex)
            {
              MessageBox.Show(ex.Message);

            }
        }

        private void BtnSearchbyidClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var listP = SearchPro(txtIDPro.Text, 0);
                _products.Clear();
                foreach (Products p in listP)
                {
                    _products.Add(p);
                }
                cmbSearchByID.IsDropDownOpen = false;
                dtgProduct.DataContext = _products;
                _products.Clear();
                foreach (var pr in listP)
                {
                    //if (p.CatalogId == selectedCatalog.id_catalog)
                    _products.Add(pr);

                }
            }
            catch (Exception ex)
            {
                
               MessageBox.Show(ex.Message);

            }
        }
        private IList<Products> SearchPro(string value, int type)
        {
            try
            {
                if (type != 0)
                {
                     var listPro =
                            _productFirstLoad.Where(x => x.ProductName.ToUpper().Contains(value.ToUpper().Trim()));
                        if (!string.IsNullOrEmpty(value))
                        {
                            listPro = listPro.Where(x => x.ProductName.ToUpper().Contains(value.ToUpper().Trim()));
                        }
                        return listPro.ToList();
                    
                }
                else
                {
                    var listPro = _productFirstLoad.Where(x => x.ProductId.ToUpper().Contains(value.ToUpper().Trim()));
                    if (!string.IsNullOrEmpty(value))
                    {
                        listPro = listPro.Where(x => x.ProductId.ToUpper().Contains(value.ToUpper().Trim()));
                    }
                    return listPro.ToList();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return new List<Products>();

        }

        private void TxtSearchbyNameTextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                var searchString = txtSearchbyName.Text;
                cmbSearchByName.Items.Clear();
                foreach (var cbmItem in from pro in _productFirstLoad where pro.ProductName.ToUpper().IndexOf(searchString.ToUpper(), StringComparison.Ordinal) >= 0 && !string.IsNullOrEmpty(searchString) select new ComboBoxItem { Content = pro.ProductName, Tag = pro.ProductIdAuto })
                {
                    cmbSearchByName.Items.Add(cbmItem);
                }
                cmbSearchByName.IsDropDownOpen = cmbSearchByName.HasItems;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void CmbSearchByNameSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var comboBox = sender as ComboBox;
                if (comboBox != null && comboBox.SelectedItem == null) return;
                var box = sender as ComboBox;
                if (box != null)
                {
                    var name = box.SelectedItem as ComboBoxItem;
                    if (name != null) txtSearchbyName.Text = name.Content.ToString();
                }
                btnSearchbyName.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        private void BtnSearchbyNameClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var listP = SearchPro(txtSearchbyName.Text, 1);
                _products.Clear();
                foreach (var p in listP)
                {
                    _products.Add(p);
                }
                cmbSearchByName.IsDropDownOpen = false;
                dtgProduct.DataContext = _products;
                _products.Clear();
                foreach (var pr in listP)
                {

                    _products.Add(pr);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
    }
}
